package com.neteast.androidclient.contact.util;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;

import com.neteast.androidclient.contact.domain.Contact;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinYinUtil {
	/**
	 * 根据联系人姓名，获取sortKey
	 * @param inStr
	 * @return
	 */
	public static char getSortKey(String inStr) {
		char[] chars=null;
		chars=inStr.toCharArray();
		if (Character.toString(chars[0]).matches("[\\u4E00-\\u9FA5]+")) {
			String[] pyArray=PinyinHelper.toHanyuPinyinStringArray(chars[0]);
			return Character.toUpperCase(pyArray[0].charAt(0));
		}else if(Character.isLetter(chars[0])){
			return Character.toUpperCase(chars[0]);
		}else{
			return '#';
		}
	}
	/**
	 * 将汉字转换成拼音
	 * @param inStr
	 * @return
	 */
	public static String hanzi2PinYin(String inStr){
		char[] chars=null;
		if (inStr==null||"".equals(inStr.trim())) {
			return null;
		}
		chars=inStr.toCharArray();
		StringBuilder result=new StringBuilder();
		
		HanyuPinyinOutputFormat format=new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		
		try {
			for(int i=0;i<chars.length;i++){
				if (Character.toString(chars[i]).matches("[\\u4E00-\\u9FA5]+")) {
					String[] pyArray = PinyinHelper.toHanyuPinyinStringArray(chars[i], format);
					result.append(pyArray[0]);
				}else {
					result.append(Character.toString(chars[i]));
				}
			}
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return result.toString();
	}
	/**
	 * 将汉字转换为拼音，只取首字母
	 * @param inStr
	 * @return
	 */
	public static String hanzi2PinYinOnlyHead(String inStr) {
		char[] chars=null;
		if (inStr==null||"".equals(inStr.trim())) {
			return null;
		}
		chars=inStr.toCharArray();
		StringBuilder result=new StringBuilder();
		for(int i=0;i<chars.length;i++){
			if (Character.toString(chars[i]).matches("[\\u4E00-\\u9FA5]+")) {
				String[] pyArray=PinyinHelper.toHanyuPinyinStringArray(chars[i]);
				result.append(pyArray[0].charAt(0));
			}else {
				result.append(chars[i]);
			}
		}
		return result.toString();
	}
	
	@SuppressWarnings("unchecked")
	public static ArrayList<String> sortName(ArrayList<String> list){
		String[] tmp = (String[])list.toArray(new String[list.size()]);  
		@SuppressWarnings("rawtypes")
		Comparator comparator = Collator.getInstance(Locale.CHINA);
		Arrays.sort(tmp, comparator);
		ArrayList<String> result = new ArrayList<String>();
		for(String str : tmp){
			result.add(str);
		}
		return result;
	}
	
}
